/*
 * @LastEditors: 七七
 * @Date: 2023-01-08 11:44:56
 */
/**
 * @description: 该文件是用于创建一个Count组件服务的reducer，reducer的本质就是一个函数
 * @param {*} preState 之前的状态
 * @param {*} action 动作对象
 * @return {*}
 */

import { INCREMENT, DECREMENT } from './constant'

const initState = 0; //默认为0
export default function countReducer(preState = initState, action) {
  // console.log('redux', preState, action);
  // 从action对象中获取type和data
  const { type, data } = action;
  // 根据type决定如何加工数据
  switch (type) {
    case INCREMENT:
      return preState + data;
    case DECREMENT:
      return preState - data;
    default:
      return preState
  }
}